var AnalyzeStatus = require('../model/AIAnalyzeData').AnalyzeStatus;
var VloudDAO = require("../dao/VloudDAO")

class VloudDomain {
    constructor(vloud_api, appkey, analyzes) {
        this._AIAnalyzes = analyzes;
        this._AppKey = appkey;
        this._Token = '';

        this.dao = new VloudDAO(vloud_api);
    }

    subScribeNotify(notifyUri) {
        var that = this;
        this.dao.subScribeNotify(notifyUri, (body)=>{
        });
    }

    startAnalyzes() {
        let that = this;
        this._callFuncAfterToken(()=>{
            that._AIAnalyzes.forEach((analyze, id) => {
                that.startAnalyze(analyze);
            });
        });
    }

    stopAnalyzes() {
        let that = this;
        this._callFuncAfterToken(()=>{
            that._AIAnalyzes.forEach((analyze, id) => {
                that.stopAnalyze(analyze);
            });
        });
    }

    startAnalyzeById(cameraId) {
        let that = this;
        let analyze = that._AIAnalyzes.get(cameraId);
        if (!analyze) {
            return false;
        }
        this._callFuncAfterToken(()=>{
            that.startAnalyze(analyze);
        });
        return true;
    }

    stopAnalyzeById(cameraId) {
        let that = this;
        let analyze = that._AIAnalyzes.get(cameraId);
        if (!analyze) {
            return false;
        }
        this._callFuncAfterToken(()=>{
            that.stopAnalyze(analyze);
        });
        return true;
    }

    getAnalyzes() {
        let arr = new Array();
        this._AIAnalyzes.forEach((analyze, id) => {
            arr.push(analyze);
        });
        return arr;
    }

    getAnalyzeById(cameraId) {
        return this._AIAnalyzes.get(cameraId);
    }

    startAnalyze(analyze) { 
        var _startAnalyze = function(analyze, body, that) {
            let parserId = body.playerId;
            analyze.streamId = parserId.split("::")[1];

            that.dao.analyze(analyze, that._AppKey, (body)=>{
                if (that._onRequestBody(body, "start analyze  [" + analyze.cameraId + "]")) {
                    analyze.status = AnalyzeStatus.Started;
                    analyze.startError = false;
                    return;
                } else {                    
                    analyze.status = AnalyzeStatus.OnError;
                    analyze.startError = true;
                    analyze.startErrorMessage = body.message;
                }
            });
        };
        var _play = function(analyze, that) {
            that.dao.play(analyze, that._Token, that._AppKey, (body)=>{                    
                if (that._onRequestBody(body, "play  [" + analyze.cameraId + "]")) {
                    _startAnalyze(analyze, body, that);
                } else {                    
                    analyze.status = AnalyzeStatus.Playing;
                    analyze.startError = true;
                    analyze.startErrorMessage = body.message;
                }
            });
        };

        let that = this;       
        this._callFuncAfterToken(()=>{
            _play(analyze, that);
        });
    }
    stopAnalyze(analyze) {
        var _releaseRoom = function(analyze, that) {
            that.dao.ReleaseRoom(analyze, that._Token, that._AppKey, (body)=>{
                if (that._onRequestBody(body, "room release  [" + analyze.cameraId + "]")) {                   
                    analyze.status = AnalyzeStatus.ReleaseRoom;
                } else {
                    analyze.startError = true;
                    analyze.startErrorMessage = body.message;
                }
            });
        };
        var _stopAnalyze = function(analyze, that) {
            that.dao.stopAnalyze(analyze, that._AppKey, (body)=>{
                if (that._onRequestBody(body, "stop analyze  [" + analyze.cameraId + "]")) {
                    analyze.status = AnalyzeStatus.Stoped;
                    _releaseRoom(analyze, that);
                } else {                    
                    analyze.startError = true;
                    analyze.startErrorMessage = body.message;
                }
            });
        };

        let that = this;
        this._callFuncAfterToken(()=>{
            _stopAnalyze(analyze, that);
        });
    }

    _callFuncAfterToken(func) { 
        let that = this;
        if (that._Token != '') {
            func();
            return;
        }       
        this.dao.requestToken(this._AppKey, (body)=>{
            console.info("request token:" + JSON.stringify(body));
            that._Token = body.token;
            func();
        });
    }

    _onRequestBody(body, remark) {
        console.error(remark + "result:" + JSON.stringify(body));
        if (body.result == "success") {
            console.error(remark +  " success");
            return true;
        }
        console.error(remark +  " failed, error:" + body.message);
        return false;
    }
}
module.exports = VloudDomain;
